Interop Structure: Should Unsigned Short be Mapped to byte[]?

Posted by Ngu Soon Hui on Stack Overflow See other posts from Stack Overflow or by Ngu Soon Hui
Published on 2010-04-09T07:14:07Z Indexed on 2010/04/09 7:23 UTC
Read the original article Hit count: 256

Filed under:
|

I have such a C++ structure:

typedef struct _FILE_OP_BLOCK
{                                                                                                                          
    unsigned short fid;     // objective file ID 
    unsigned short offset;  // operating offset
    unsigned char len;      // buffer length(update)
                            // read length(read)        
    unsigned char buff[MAX_BUFF_SIZE];
} FILE_OP_BLOCK;

And now I want to map it in .Net. The tricky thing is that the I should pass a 2 byte array for fid, and integer for len, even though in C# fid is an unsigned short and len is an unsigned char

I wonder whether my structure ( in C#) below is correct?

   public struct File_OP_Block 
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public byte[] fid;

        public ushort offset;

        public byte length;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 240)]
        public char[] buff;
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about interop